// src.module.activityPage.module.js

var MongoDB = require('./DB/mongodb');
var mongoUtil = require('../helper/MongoUtil');
var dateUtil = require('../helper/DateformatHelper.js');

exports.save = function (req, res, callback) {
	var params={};
	if(req.query._id){
		var update_fields = {_id:req.query._id,activity_template:req.body.activity_template,activity_content:JSON.stringify(req.body),activity_ctime:dateUtil.getCurrentTime()};
		MongoDB.update('activity_page', conditions, update_fields,function(err, result){
			callback(err, result);
		})
	}else{
		var fields = {activity_template:req.body.activity_template,activity_content:JSON.stringify(req.body),activity_ctime:dateUtil.getCurrentTime()};
		MongoDB.save('activity_page', fields,function(err, result){
			callback(err, result);
		})
	}
};

exports.delete = function (req, res, callback) {
	var conditions = req.query._id;
	MongoDB.remove('activity_page', conditions,function(err, result){
		callback(err, result);
	});
};

exports.queryById = function (req, res, callback) {
	var conditions = {};
	conditions._id = req.query._id;
	conditions.is_del = 0;
	MongoDB.findOne('activity_page', conditions, function (err, result) {
		callback(err, result);
	});
};

exports.queryByPage = function (req, res, callback) {
	req.page = {'currentPage':req.query.currentPage||1,'pageSize':req.query.pageSize||10};
	req.page.pageSize = parseInt(req.page.pageSize);
	var conditions = {}, options ={}, p = req.query;
	options.limit = req.page.pageSize;

	if(p.activity_status) {
		conditions.activity_status = p.activity_status;
	}
	if(p._id){
		conditions._id = p._id;
	}
	if (p.activity_name){
		conditions.activity_name = mongoUtil.like(p.activity_name);
	}

	MongoDB.where('activity_page',conditions,options,function(err, result){
		callback(err, result);
	});
};

exports.queryAll = function (req, res, callback) {
	var conditions = {};
	var fields ="_id activity_id parent_id activity_name activity_title activity_keyword activity_desc activity_template activity_content activity_page_url activity_display activity_order activity_type description activity_status activity_ctime activity_cuser";
	MongoDB.find('activity_page',conditions,fields,function(err, result){
		callback(err, result);
	});
};